﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ICDA.Web.Models;
using ICDA.DataModel;
using ICDA.DataModel.Abstract;
using ICDA.DataModel.Concrete;
using ICDA.DataModel.Entities;
using ICDA.Web.Helper;
using System.Configuration;

namespace ICDA.Web.Controllers
{
    public class LeaveController : Controller
    {
        private static LeaveModel _LeaveModel;
        IRepository _repository;
        DatetTimeHelper dtHelper = new DatetTimeHelper();
        public static LeaveModel leaveModel
        {
            get
            {
                if (_LeaveModel == null)
                {
                    _LeaveModel = new LeaveModel();
                }
                return _LeaveModel;
            }
        }
        #region Constructors
        public LeaveController()
            : this(new DBrepository())
        {
        }
        public LeaveController(IRepository repository)
        {
            _repository = repository;
        }
        #endregion
        //
        // GET: /LeaveManagement/
        
        public ActionResult Index()
        {
            return View();
        }

        //GET: /LeaveManagement/
        public ActionResult LeaveApply()
        {
            if (Session["UID"] != null)
            {
                return View();
            }
            else
            {
                return RedirectToAction("LogOn", "Account");
            }
        }
        [HttpPost]
        public ActionResult LeaveApply(LeaveModel model)
        {
            try
            {
                if (Session["UID"] != null)
                {
                    string uid = Session["UID"].ToString().Trim();
                    LeaveTables leave = new LeaveTables
                    {
                        UserID = uid,
                        LeaveStartDate = dtHelper.FormatDateTime(model.LeaveStartDate),
                        LeaveEndDate = dtHelper.FormatDateTime(model.LeaveEndDate.Date),
                        LeaveType = model.LeaveType ? "Leave" : "Training",
                        LeaveName = model.LeaveName.Trim(),
                        DayType = model.DayType ? "FullDay" : "HalfDay",
                    };
                    _repository.AddLeave(leave);
                    if (_repository.SaveChanges())
                    {
                        if (ConfigurationManager.AppSettings["MailService"] == "True")
                        {
                            string subject = Session["UserName"].ToString() + " " + "will be on " + model.LeaveType + " " + "from "
                                + " " + model.LeaveStartDate + " " + "to " + " " + model.LeaveEndDate + " " + "<EOM>";
                            //Helper.Email.SendMail("", "", "");
                        }
                        return RedirectToAction("ViewLeave");
                    }
                    else
                    {
                        ModelState.AddModelError("Exception", "Unable to save changes");
                        return View(model);
                    }
                }
                else
                {
                    return View(model);
                }
            }
            catch (Exception ex)
            {
                ViewData["Description"] = ex.Message;
                ViewData["Title"] = "Oops. We're sorry. An error occurred ............";  
                return View("Error"); 
            }
            
        }

        public ActionResult ViewLeave()
        {
            try
            {
                if (Session["UID"] != null)
                {
                    string uid = Session["UID"].ToString().Trim();
                    List<LeaveModel> lm = new List<LeaveModel>();
                    IEnumerable<LeaveTables> leavetab = _repository.leave.Where(u => u.UserID == uid).ToList();
                    foreach (LeaveTables items in leavetab)
                    {
                        LeaveModel lmodel = new LeaveModel();
                        lmodel.ID = items.ID;
                        lmodel.LeaveStartDate = Convert.ToDateTime(String.Format("{0:d}", items.LeaveStartDate));
                        lmodel.LeaveEndDate = Convert.ToDateTime(String.Format("{0:d}", items.LeaveEndDate));
                        lmodel.LeaveName = items.LeaveName.Trim();
                        lmodel.LeaveType = string.Equals(items.LeaveType.Trim(), "Leave");
                        lmodel.DayType = string.Equals(items.DayType.Trim(), "FullDay");
                        lm.Add(lmodel);
                    }
                    return View(lm);
                }
                else
                {
                    return RedirectToAction("LogOn", "Account");
                }
            }
            catch (Exception ex)
            {
                ViewData["Description"] = ex.Message;
                ViewData["Title"] = "Oops. We're sorry. An error occurred ............";
                return View("Error"); 
            }
        }

        [HttpGet]
        public ActionResult LeaveEdit(int id)
        {
            try
            {
                if (Session["UID"] != null)
                {
                    LeaveTables leave = _repository.leave.Where(u => u.ID == id).FirstOrDefault();
                    leaveModel.LeaveStartDate = Convert.ToDateTime(String.Format("{0:d}", leave.LeaveStartDate));
                    leaveModel.LeaveEndDate = Convert.ToDateTime(String.Format("{0:d}", leave.LeaveEndDate));
                    leaveModel.LeaveName = leave.LeaveName.Trim();
                    leaveModel.DayType = string.Equals(leave.DayType.Trim(), "FullDay");
                    leaveModel.LeaveType = string.Equals(leave.LeaveType.Trim(), "Leave");
                    leaveModel.UserID = Session["UID"].ToString().Trim();
                    return View(leaveModel);
                }

                else
                {
                    return RedirectToAction("LogOn", "Account");
                }
            }
            catch (Exception ex)
            {
                ViewData["Description"] = ex.Message;
                ViewData["Title"] = "Oops. We're sorry. An error occurred ............";
                return View("Error"); 
            }
        }

        [HttpPost]
        public ActionResult LeaveEdit(LeaveModel model, string actionType)
        {
            try
            {
                if (Session["UID"] != null && actionType == "Submit")
                {
                    LeaveTables leave = _repository.leave.Where(u => u.ID == model.ID).FirstOrDefault();
                    leave.LeaveStartDate = Convert.ToDateTime(String.Format("{0:d}", model.LeaveStartDate.Date));
                    leave.LeaveEndDate = Convert.ToDateTime(String.Format("{0:d}", model.LeaveEndDate.Date));
                    leave.LeaveName = model.LeaveName.Trim();
                    leave.LeaveType = model.LeaveType ? "Leave" : "Training";
                    leave.DayType = model.DayType ? "FullDay" : "HalfDay";
                    _repository.AddLeave(leave);
                    if (_repository.SaveChanges())
                    {
                        return RedirectToAction("ViewLeave");
                    }
                    else
                    {
                        return View(model);
                    }
                }
                else if (Session["UID"] != null && actionType == "Cancel")
                {
                    return RedirectToAction("ViewLeave");
                }
                else
                {
                    return RedirectToAction("LogOn", "Account");
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("Exception", ex.Message);
                return View();
            }
        }
        public ActionResult DeleteLeave(int id)
        {
            try
            {
                string uid = Session["UID"].ToString().Trim();
                LeaveTables leave = _repository.leave.Where(u => u.ID == id).FirstOrDefault();
                _repository.DeleteLeave(leave);
                if (_repository.SaveChanges())
                {
                    return RedirectToAction("ViewLeave");
                }
                else
                {
                    return View();
                }
            }
            catch (Exception ex)
            {
                ViewData["Description"] = ex.Message;
                ViewData["Title"] = "Oops. We're sorry. An error occurred ............";
                return View("Error"); 
            }
        }

        public ActionResult LeaveHistory()
        {
            try
            {
                if (Session["UID"] != null)
                {
                    string uid = Session["UID"].ToString().Trim();
                    List<LeaveModel> lm = new List<LeaveModel>();
                    IEnumerable<LeaveTables> leavetab = _repository.leave.Where(u => u.UserID == uid).ToList();
                    foreach (LeaveTables items in leavetab)
                    {
                        LeaveModel lmodel = new LeaveModel();
                        lmodel.ID = items.ID;
                        lmodel.LeaveStartDate = Convert.ToDateTime(String.Format("{0:d}", items.LeaveStartDate));
                        lmodel.LeaveEndDate = Convert.ToDateTime(String.Format("{0:d}", items.LeaveStartDate));
                        lmodel.LeaveName = items.LeaveName.Trim();
                        lmodel.LeaveType = string.Equals(items.LeaveType.Trim(), "Leave");
                        lmodel.DayType = string.Equals(items.DayType.Trim(), "FullDay");
                        lm.Add(lmodel);
                        lmodel.LeaveM = lm;
                    }

                    return View(lm);
                }
                else
                {
                    return View();
                }
            }
            catch (Exception ex)
            {
                ViewData["Description"] = ex.Message;
                ViewData["Title"] = "Oops. We're sorry. An error occurred ............";
                return View("Error"); 
            }
        }
    }
}
